<template>
	<view class="content" catchtouchmove="true">
			<scroll-view class="tab " id="tab" :show-scrollbar="false" :scroll-x="true" :scroll-into-view="tabScrollInto">
				<view class="tabContent" style="display: flex; flex-direction: column;">
					<view style="display: flex;justify-content: space-around;">
						<view class="tabItem" v-for="(tab,tabItemIndex) in tabList" :key="tab.id" :id="tab.id" :data-id="tabItemIndex"
							:data-current="tabItemIndex" @click="pressScrollViewItem">
							<text class="tabItemTitle"
								:class="tabIndex==tabItemIndex ? 'tabItemTitleActive' : ''"
								:style="{width:tabList[tabItemIndex].width + 'px'}"
								>{{tab.title}}</text>
						</view>
					</view>
					<view class="tabLineView">
						<view class="tabLine" :class="isPress ? 'tabLineActive':'tabLineActive'"
							:style="{left: tabItemLineLeft + 'px', width: tabItemLineWidth + 'px'}"></view>
					</view>
				</view>
			</scroll-view>
			<navigator url="../newsAdd/newsAdd">
				<view class="u-text-center add-container">
					+ 新增
				</view>
			</navigator>
		<swiper class="childPageView" :current="tabIndex" :duration="250" @change="swiperChange"
			@animationfinish="swiperChangeEnd" @onAnimationEnd="swiperChangeEnd" :style="{height: swiperHeight+'px'}">

			<swiper-item class="childPageViewItem" v-for="(page, childPageViewItemIndex) in tabList" :key="childPageViewItemIndex">
				<!-- 未读 -->
				<scroll-view v-if="childPageViewItemIndex == 0" scroll-y="true" :style="{height: swiperHeight+'px'}">
					<view class="" v-if="infoList0">
						<view class="news-container" @click="becomeReplied(data)" v-for="(data,Index) in item1Data" :key="Index" >
							<view class="news-top" >
								<view class="" style="font-size: 15px; font-weight: 700; color: #303133;">
									{{data.createBy_dictText}}
								</view>
								<view class="" style="display: flex;">
									<view class="" style="font-size:13px;color: #303133;">
										（{{data.status_dictText +'信息' }}）
									</view>
									<view class="">
										{{data.createTime}}
									</view>
								</view>
							</view>
							<view class="u-line-2" style="font-size: 14px;">
								【{{data.title}}】{{data.content}}
							</view>
						</view>
					</view>
					<view class="" v-if="!infoList0" style="margin-top: 40px; width: 100%; font-size: 20px; color:#303133; text-align: center;">
						暂无信息...
					</view>
				</scroll-view>

				<!-- 已读 -->
				<scroll-view v-if="childPageViewItemIndex != 0" scroll-y="true" :style="{height: swiperHeight+'px'}">
					<view class="" v-if="infoList1">
						<view class="news-container" @click="toNewsReply(data)" v-for="(data,Index) in item2Data" :key="Index" >
							<view class="news-top" >
								<view class="" style="font-size: 15px; font-weight: 700; color: #303133;">
									{{data.createBy_dictText}}
								</view>
								<view class="" style="display: flex;">
									<view class="" style="font-size:13px;color: #303133;">
										（{{data.status_dictText +'信息' }}）
									</view>
									<view class="">
										{{data.createTime}}
									</view>
								</view>
							</view>
							<view class="u-line-2" style="font-size: 14px;">
								【{{data.title}}】{{data.content}}
							</view>
						</view>
					</view>
					<view class="" v-if="!infoList1" style="margin-top: 40px; width: 100%; font-size: 20px; color:#303133; text-align: center;">
						暂无信息...
					</view>
				</scroll-view>

			</swiper-item>
		</swiper>

	</view>
</template>

<script>
	import newsIndex from '../../api/newsIndex.js'

	export default {
		components: {
		},
		data() {
			return {
				tabList: [{
					id: "tabItem0",
					title: '未读',
					width:100,
				}, {
					id: "tabItem1",
					title: '已读',
					width:100,
				}],
				infoList0:'',
				infoList1:'',
				tabIndex: 0,
				tabScrollInto: "",
				tabListSize: {},
				tabItemLineLeft: 0,
				tabItemLineWidth: 0,
				isPress: false,
				swiperHeight: 0,
				//未读信息
				item1Data: [{
					createBy_dictText:'发件人',
					createTime:'时间',
					status_dictText: '信息重要程度',
					title: '标题',
					content: "信息内容"
				}],
				//已读信息
				item2Data: [{
					createBy_dictText:'发件人',
					createTime:'时间',
					status_dictText: '信息重要程度',
					title: '标题',
					content: "信息内容"
				}],
			}
		},
		onLoad() {
			let system = uni.getSystemInfoSync();
			this.swiperHeight = system.windowHeight - 44;
			
			console.log(this.$store.state)
		},
		onReady() {
			this.setTabItemDistance();
			this.setTabSelect(this.tabIndex);
			//发起请求
			// this.getInformation()
		},
		onShow: function () {
			this.getInformation()
		},
		methods: {
			getInformation(){
				//获取未读信息
				newsIndex.Unread({
					'X-Access-Token': this.$store.state.token
				}).then(res =>{
					console.log(res)
					if(!res.data.result){
						this.infoList0 = false
					}else{
						this.infoList0 = true
						//未读信息赋值
						this.item1Data = res.data.result.records
					}
					
				}).catch(err => {
					console.log(err)
				})
				//获取已读信息
				newsIndex.Read({
					'X-Access-Token': this.$store.state.token
				}).then(res =>{
					if(!res.data.result){
						this.infoList1 = false
					}else{
						this.infoList1 = true
						//已读信息赋值
						this.item2Data = res.data.result.records
					}
				}).catch(err => {
					console.log(err)
				})
			},
			//消息变为已回复
			becomeReplied(data){
				newsIndex.replied({
					'X-Access-Token': this.$store.state.token
				},{'id': data.id},
				).then(res =>{
					console.log('已回复')
				}).catch(err => {
					console.log(err,'未回复')
				})
			this.$Router.push({ path: '/pages/newsReply/newsReply', query: {message: data}})
			},
			//跳转页面
			toNewsReply(data){
				console.log(data)
				this.$Router.push({ path: '/pages/newsReply/newsReply', query: {message: data}})
			},
			
			//以下方法组件自带
			pressScrollViewItem(e) {
				let index = e.target.dataset.current || e.currentTarget.dataset.current;
				this.isPress = true;
				this.setTabSelect(index);
			},
			swiperChange(e) {
				let index = e.target.current || e.detail.current;
				this.tabIndex = index;
				this.setTabSelect(this.tabIndex);
			},
			setTabItemDistance() {
				var queryTabSize = uni.createSelectorQuery().in(this);
				for (var i = 0; i < this.tabList.length; i++) {
					queryTabSize.select('#' + this.tabList[i].id).boundingClientRect();
				}
				queryTabSize.exec(rects => {
					rects.forEach((rect) => {
						this.tabListSize[rect.dataset.id] = rect;
					})
					this.setTabItemLinePosition(this.tabListSize[this.tabIndex].left, this.tabListSize[this
							.tabIndex]
						.width);
				});
			},
			setTabItemLinePosition(left, width) {
				this.tabItemLineLeft = left;
				this.tabItemLineWidth = width;
			},
			swiperChangeEnd(e) {
				this.setTabItemLinePosition(this.tabListSize[this.tabIndex].left, this.tabListSize[this.tabIndex].width);
			},
			setTabSelect(index) {
				this.tabIndex = index;
				this.tabScrollInto = this.tabList[index].id;
			},

		}
	}
</script>

<style lang="scss">
	.content {
		/* flex: 1; */
		display: flex;
		flex-direction: column;
		overflow: hidden;
		background-color: #ffffff;
	}
	.add-container{
		margin:5px 0;
		width: 100%;
		height: 40px;
		line-height: 40px;
		font-size: 16px;
		background-color: $u-type-primary;
		color: $u-type-info-light;
	}
	.news-container{
		width: 90%; 
		// height: 150rpx; 
		margin: 0 auto 10rpx; 
		padding: 5px 10px;
		background-color: #ffffff;
		border-bottom: 1px solid #dcdcdc;
		font-size: 12px;
		color: $u-type-info;
	}
	.news-top{
		display: flex;
		justify-content: space-between;
	}
	.tab {
		/* #ifdef APP-PLUS */
		width: 100vw;
		/* #endif */
		height: 44px;
		display: flex;
		flex-direction: row;
		/* #ifndef APP-PLUS */
		white-space: nowrap;
		/* #endif */
	}

	/* 隐藏滚动条 */

	.tab ::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}

	.tabLineView {
		position: relative;
		height: 2px;
		background-color: transparent;
		
	}

	.tabLine {
		position: absolute;
		top: 0;
		bottom: 0;
		width: 0;
		background-color: $u-type-primary;
	}

	.tabLineActive {
		transition-duration: 0.1s;
		transition-property: left;
	}

	.childPageView {
		display: flex;
		flex-direction: column;
	}

	.tabItem {
		/* #ifndef APP-PLUS */
		/* display: inline-block; */
		/* #endif */
		display: flex;
		
	}

	.tabItemTitle {
		color: #333333;
		font-size: 15px;
		height: 42px;
		line-height: 40px;
		display: flex;
		flex-wrap: nowrap;
		align-items: center;
		justify-content: center;
	}

	.tabItemTitleActive {
		color: $u-type-primary;
	}

	.childPageViewItem {
		flex: 1;
		flex-direction: column;
	}
</style>
